#include "login.h"
#include "ui_login.h"
#include"QPainter"
#include "qsettings.h"
#include "HRVPro/hrvdataprocess.h"
#include "Config/constant.h"

Login::Login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Login)
{
    ui->setupUi(this);
    Series_HR = new QLineSeries;
    xAxis_HR = new QValueAxis();
    yAxis_HR = new QValueAxis();
    InitialAxis_Chart_HR();
    chart=CreateLineChart_HR();

    QSettings setting("./Configuration.ini",QSettings::IniFormat);
    setting.beginGroup("Config");
    QString reportName=setting.value("ReportName").toString();
    setting.endGroup();
    ui->label_SL_Title->setText(reportName+"（生理参数）分析报告");
}

Login::~Login()
{
    delete Series_HR;
    delete xAxis_HR;
    delete yAxis_HR;
    delete chart;
    delete ui;
}

void Login::setValue(DataBeans dataBean)
{
    QSettings *setting=new QSettings("./Configuration.ini",QSettings::IniFormat);
    setting->beginGroup("Config");
    QString JGName=setting->value("User").toString();
    setting->endGroup();
    delete setting;
    HRVDataProcess hrvdataPro;
    HRVIndex hi=hrvdataPro.Json2HRVData(dataBean.hrvIndexResults);
    ui->label_DW->setText(JGName);
    ui->label_display_ID->setText(dataBean.UserID);
    ui->label_display_name->setText(dataBean.UserName);
    ui->label_display_age->setText(dataBean.UserAge+"/"+dataBean.UserSex);
    ui->label_display_time->setText(dataBean.MeasureTime);
    ui->label_display_max->setText(QString::number(hi.sl.MaxHrt));
    ui->label_display_min->setText(QString::number(hi.sl.MinHrt));
    ui->label_display_aver->setText(QString::number(hi.sl.MeanHrt));
    ui->label_display_unnor->setText(QString::number(hi.sl.AbnormalHeartNum));
    ui->label_display_ox->setText(QString::number(hi.sl.ox));
    ui->label_display_perfusion->setText(QString::number(hi.sl.Perfusion,'f',2));
    ui->textEdit_SL_Res->setText(hi.SLResults);

    ui->widget_wave->setChart(chart);
    ui->widget_wave->setRenderHint(QPainter::Antialiasing,true);
    setHRData(hi.time.RR);

}


void Login::InitialAxis_Chart_HR()
{

    yAxis_HR->setRange(30, 120);
    xAxis_HR->setRange(0, 360);
    xAxis_HR->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_HR->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_HR->setTitleText("心率曲线图");
    yAxis_HR->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_HR->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_HR->setGridLineVisible(false);
    yAxis_HR->setGridLineVisible(true);
    yAxis_HR->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_HR->setLineVisible(false);
    xAxis_HR->setVisible(false);
    yAxis_HR->setTitleText("心率(Beat/Min)");
    yAxis_HR->setVisible(true);
}
QChart* Login::CreateLineChart_HR()
{
    QChart *chart = new QChart();
    Series_HR->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    chart->addSeries(Series_HR);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_HR, Qt::AlignBottom);
    chart->addAxis(yAxis_HR, Qt::AlignLeft);
    Series_HR->attachAxis(xAxis_HR);
    Series_HR->attachAxis(yAxis_HR);
    return chart;
}

void Login::setHRData(QList<int>rr)
{

    Series_HR->clear();
    int rr_Len=rr.size();
    xAxis_HR->setRange(0, rr_Len);
    for(int i=0;i<rr_Len;i++)
    {
        Series_HR->append(QPointF(i,60*1000/rr.at(i)));
    }
}
